#!/bin/sh


if test $# -gt 0
then
    echo "Usage: $0"
    exit 1
fi

# this is going to be ran from within the build directory
exportUserspace=${PWD}
umemPrefix=${exportUserspace}/umem-1.0-prefix

# if already built, we can exit gracefully...
if test -e $umemPrefix/include/umem.h -a -e $umemPrefix/lib/libumem.a
then
    echo "umem already built previously to $umemPrefix"
    exit 0
fi

# we need to build umem.  where is the source?
       
# assume we are in KERN/userspace/build and need ../umem-1.0
umemSrcPath=${PWD}/..
if test ! -d $umemSrcPath/umem-1.0
    then
# someone other than us may be building this with their own unique
#  directory structure, but they need to tell us stuff...
    echo "Which directory contains the umem source code directory umem-1.0?"
    read umemPath
    if test ! -d $umemSrcPath/umem-1.0
        then
        echo "Sorry I cannot see $umemSrcPath/umem-1.0"
        exit 1
    fi
fi

# ok we found umem-1.0
echo "Building umem-1.0..."
cd $umemSrcPath/umem-1.0
autoconf
./configure --prefix=$umemPrefix
make
make install


# soft-link items in prefix/lib to user_lib and 
#  from prefix/include to user_include

cd $exportUserspace
if test ! -d user_lib
    then
    mkdir user_lib
fi
cd user_lib
ln -s $umemPrefix/lib/libumem.a
ln -s $umemPrefix/lib/libumem.la
ln -s $umemPrefix/lib/libumem.so.0 libumem.so
ln -s $umemPrefix/lib/libumem.so.0
ln -s $umemPrefix/lib/libumem_malloc.a
ln -s $umemPrefix/lib/libumem_malloc.la
ln -s $umemPrefix/lib/libumem_malloc.so.0 libumem_malloc.so
ln -s $umemPrefix/lib/libumem_malloc.so.0
cd $exportUserspace/user_include
ln -s $umemPrefix/include/umem.h
if test ! -d $exportUserspace/user_include/sys
    then
    mkdir $exportUserspace/user_include/sys
fi
cd $exportUserspace/user_include/sys
ln -s $umemPrefix/include/sys/vmem.h

